import Vue from 'vue'
import VueRouter from 'vue-router'
import { initMenu } from '../api/menu'
import store from '../store'

import Login from '@/views/login/Login.vue'

import Home from '@/views/Home.vue'
import OnlineChat from '@/views/chat/OnlineChat.vue'
import UserInfo from '@/views/UserInfo.vue'
import { getRequest } from '../utils/request'

Vue.use(VueRouter)

const routes = [
  { path: '/',redirect:'/login'},
  { path: '/login',component: Login,hidden:true },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children:[
      {
        path: '/chat',
        name: '在线聊天',
        component: OnlineChat,
      },
      {
        path: '/userinfo',
        name: '个人中心',
        component: UserInfo,
      }
    ]
  }
  
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
  const token = sessionStorage.getItem('token')
  if(token){
    //初始化菜单
    initMenu(router,store)
    // if(!window.sessionStorage.getItem('user')){
    //   getRequest('/admin/info').then(data => {
    //     if(data) {
    //       window.sessionStorage.setItem('user',JSON.stringify(data))
    //       next()
    //     }
    //   })
    // }
    next()
  }else{
    // console.log(to);
    if(to.path == '/' || to.path == '/login'){
      // console.log(22);
      next()
    }else{
      next('/login')
    }
  }
})
export default router
